/**
 * @author Artem Gruzdev
 * @class Ext.ux.form.HtmlEditor.Link
 * @extends Ext.util.Observable
 * <p>A plugin that modifies text copied from Excel Worksheet and pasted into textearea</p>
 */
Ext.ux.form.HtmlEditor.PasteExcel = Ext.extend(Ext.util.Observable, {
	constructor: function(config){
        this.tableCls = config.tableCls;
        Ext.ux.form.HtmlEditor.PasteExcel.superclass.constructor.call(this, config)
    },
    init: function(cmp){
        this.cmp = cmp;
        this.cmp.on('render', this.onRender, this);
    },
    onRender: function(){
        var btn = this.cmp.getToolbar().addButton({
            iconCls: 'x-edit-paste-excel-txt',
            handler: function(){
                var sel = this.cmp.getSelectedText();
                if (!this.pasteWindow) {
                    this.pasteWindow = new Ext.Window({
                        title: "Вставка таблицы Excel",
                        closeAction: 'hide',
                        width: 500,
                        height: 420,
						layout: "anchor",
                        items: [{
                            xtype: 'form',
							anchor: '100% 100%',
                            itemId: 'paste-excel',
                            border: false,
                            plain: true,
                            bodyStyle: 'padding: 10px;',
                            labelWidth: 100,
                            labelAlign: 'right',
                            items: [{
                                xtype: 'textarea',
                                fieldLabel: "Скопируйте текст из Excel",
                                name: 'exceltxt',
                                anchor: '100% 100%',
                                value: '',
								allowBlank: false
                            }]
                        }],
                        buttons: [{
                            text: "ОК",
                            handler: function(){
                                var frm = this.pasteWindow.getComponent('paste-excel').getForm();
                                if (frm.isValid()) {
									var s=frm.findField('exceltxt').getValue();
									try{
										var i = s.indexOf("\n");
										if (i==-1) throw "No header";
										var re = /\t/g;
										var h = '<table' + ((this.tableCls!=undefined)? ' class="'+this.tableCls+'">' : '>')
										h += "<thead><tr><th>"+s.substr(0,i).replace(re,"</th><th>")+"</th></tr></thead>\n";
										s = s.substr(i+1);
										i = s.lastIndexOf("\n");
										if (i>-1) s=s.substr(0,i);
										s = s.replace(re,"</td><td>");
										re = /\n/g;
										s = "<tbody><tr><td>"+s.replace(re,"</td></tr>\n<tr><td>")+"</td></tr></tbody></table>";
										s = h+s;
										this.cmp.insertAtCursor(s);
										this.pasteWindow.hide();
									}catch(e){alert(e);}
									
                                }
                            },
                            scope: this
                        }, {
                            text: "Отмена",
                            handler: function(){this.pasteWindow.hide();},
                            scope: this
                        }],
                    });
                    this.pasteWindow.show();
                } else {
                    this.pasteWindow.show();
                }
            },
            scope: this
            //,tooltip: this.langTitle
        });
    }
});
